﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class DemoDataList_Product : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadCategory();
            LoadProduct();
        }
    }

    private void LoadCategory()
    {
        SqlDataAdapter da = new SqlDataAdapter("select id,name from category", DataAccess.ConnectionString);
        DataSet ds = new DataSet();
        da.Fill(ds);
        rptCategory.DataSource = ds.Tables[0];
        rptCategory.DataBind();
    }

    private void LoadProduct()
    {
        string cid = (Request["cid"]!=null)?Request["cid"]:"0";
        SqlDataAdapter da = new SqlDataAdapter("select pid,pname,price,picture from product where cateid=@cid", DataAccess.ConnectionString);
        da.SelectCommand.Parameters.AddWithValue("@cid", cid);
        DataSet ds = new DataSet();
        da.Fill(ds);
        //xu ly phan trang
        PagedDataSource page = new PagedDataSource();
        page.AllowPaging = true;
        page.DataSource = ds.Tables[0].DefaultView;
        page.PageSize = 6;

        int p = 0;
        try
        {
            p = int.Parse(Request["p"])-1;
        }
        catch
        {
        }
        //chi dinh trang hien thi
        page.CurrentPageIndex = p;
        //tao link
        if (page.PageCount >1)
        {
            for (int i = 1; i <= page.PageCount; i++)
            {
                if (i != p + 1)
                    Label3.Text += " <a href='?cid=" + Request["cid"] + "&p=" + i + "'> " + i + " </a> ";
                else
                    Label3.Text += i + " ";
            }
        }
        //hien thi du lieu dung trang do
        dlstProduct.DataSource = page;
        dlstProduct.DataBind();
    }
}
